package com.woniuxy.controller;

import com.woniuxy.entity.User;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: 曾令忠
 * @Description:
 * @Date: 2020/7/3 0003-上午 10:46
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    @ResponseBody
    public String login(User user){
        Subject subject = SecurityUtils.getSubject();
        if(!subject.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUname(),user.getPwd());
            try {
                subject.login(token);
                //将账户信息添加到session
                subject.getSession().setAttribute("user",user.getUname());
            }catch (Exception e){
                e.printStackTrace();
                return "fail";
            }
        }
        return "success";
    }
}
